package com.skyz.wrap.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.skyz.wrap.activity.MyCaptureActivity;
import com.skyz.wrap.bean.QrCode;
import com.skyz.wrap.manager.RouteManager;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;
import com.yzq.zxinglibrary.encode.CodeCreator;

public class ZxingUtil {
    private final static int REQUEST_CODE_SCAN=111;
    private ZxingUtil(){

    }
    private static ZxingUtil instance = new ZxingUtil();

    public static ZxingUtil getInstance(){
        return  instance;
    }

    public void toCaptureActivityForResult(Activity activity){
        Intent intent = new Intent(activity, MyCaptureActivity.class);
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, getConfig());
        activity.startActivityForResult(intent, REQUEST_CODE_SCAN);
    }

    public void toCaptureActivityForResult(Fragment fragment){
        Intent intent = new Intent(fragment.getActivity(), MyCaptureActivity.class);
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, getConfig());
        fragment.startActivityForResult(intent, REQUEST_CODE_SCAN);
    }

    private ZxingConfig getConfig(){
        /*ZxingConfig是配置类
         *可以设置是否显示底部布局，闪光灯，相册，
         * 是否播放提示音  震动
         * 设置扫描框颜色等
         * 也可以不传这个参数
         * */
        ZxingConfig config = new ZxingConfig();
        config.setPlayBeep(true);//是否播放扫描声音 默认为true
        config.setShake(true);//是否震动  默认为true
        config.setDecodeBarCode(true);//是否扫描条形码 默认为true
        config.setShowAlbum(true);
//        config.setReactColor(R.color.colorAccent);//设置扫描框四个角的颜色 默认为白色
//        config.setFrameLineColor(R.color.colorAccent);//设置扫描框边框颜色 默认无色
//        config.setScanLineColor(R.color.colorAccent);//设置扫描线的颜色 默认白色
        config.setFullScreenScan(false);//是否全屏扫描  默认为true  设为false则只会在扫描框中扫描
        return config;
    }

    /**
     * 获取扫码结果 进入扫一扫页面，识别商家收款码后，进入付款页面
     * @param requestCode
     * @param resultCode
     * @param data
     * @return
     */
    public String onActivityResult(Activity activity,int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SCAN && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                String content = data.getStringExtra(Constant.CODED_CONTENT);
                if (TextUtils.isEmpty(content)){
                    return "";
                }
                if (content.startsWith("yz:crowdfunding")){
                    RouteManager.getInstance().showActivity(activity,"RaiseMineActivity");
                    return content;
                }
                QrCode result =null;
                try {
                     result = new Gson().fromJson(content, QrCode.class);
                }catch (JsonSyntaxException e){
                    e.printStackTrace();
                }

                if (result!=null){
                    if (!TextUtils.isEmpty(result.getUid())){
                        Bundle bundle = new Bundle();
                        bundle.putString("UID",result.getUid());
                        RouteManager.getInstance().showActivity(activity,"ShopPayActivity",bundle);
                    }else if (!TextUtils.isEmpty(result.getTeamId())){
                    }

                }
                return content;
            }
        }
        return "";
    }


    public void createQRCode(Context context, String content, int w, int h, String imgUrl,CallBack callBack){
        Glide.with(context).asBitmap().load(imgUrl).into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                Bitmap bm= CodeCreator.createQRCode(content,w,h,resource);
                if (bm!=null){
                    callBack.onSuc(bm);
                }else {
                    callBack.onError();
                }
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);
                Bitmap bm= CodeCreator.createQRCode(content,w,h,null);
                if (bm!=null){
                    callBack.onSuc(bm);
                }else {
                    callBack.onError();
                }
            }
        });
    }

    public static interface CallBack{
        void onSuc(Bitmap bm);
        void onError();
    }
}
